/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import pojo.Activities;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author A7med Kotb
 */
public class ActivitiesDao
{
    String driverName = "com.mysql.jdbc.Driver";
    Connection con;
    public ActivitiesDao()
    {
        try
        {
            Class.forName(driverName).newInstance();
            con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/dimensionproject", "root","root");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
/////////////////////////////////////////Adding new Activity///////////////////////////////    
    public void add(Activities act)
    {
        try
        {
            PreparedStatement pstmt = con.prepareStatement("insert into activities values(?,?)") ;
            pstmt.setInt(1,act.getAId());
            pstmt.setString(2,act.getDescription());
            pstmt.execute() ;
        }
        catch(SQLException ex1)
        {
            ex1.printStackTrace();
        }
    }
//////////////////////////////////////delete Activity/////////////////////////////////////////
    public void delete(Activities act)
    {
        try
        {
            PreparedStatement pstmt = con.prepareStatement("delete from activities where a_id=?") ;
            pstmt.setInt(1,act.getAId());
            pstmt.execute() ;
        }
        catch(SQLException ex1)
        {
            ex1.printStackTrace();
        }
    }
//////////////////////////////////////update Activity//////////////////////////////////////////////////
    public void update(Activities act)
    {
        try 
        {
            boolean first=false;
            String updatetxt="";
            if(act.getDescription()!=null)
            {   first=true;
                updatetxt="description='"+act.getDescription()+"'";
            }
            PreparedStatement ps=con.prepareStatement("update activities set "+updatetxt+" where a_id=?");
            ps.setInt(1, act.getAId());
            ps.executeUpdate();
            
        } 
        catch (SQLException ex) 
        {
            ex.printStackTrace();
        }
    
    }
    
}
